package com.berchen.管道;

import org.junit.Test;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;

public class TestPipe {

    @Test
    public void test1() throws IOException {

        // 获取管道
        Pipe open = Pipe.open();

        ByteBuffer buf = ByteBuffer.allocate(1024);
        buf.put("管道发送数据".getBytes());
        buf.flip();

        // 将缓冲区中的数据写入管道
        Pipe.SinkChannel sinkChannel = open.sink();
        sinkChannel.write(buf);

        buf.flip();

        // 读取缓冲区中的数据
        Pipe.SourceChannel sourceChannel = open.source();
        sourceChannel.read(buf);

        System.out.println(new String(buf.array()));

        sourceChannel.close();
        sinkChannel.close();
    }
}
